package yago;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Match {
	
	public static ArrayList<String> timeStamp2dates(Timestamp d){
		//one time in yago correspondes to multiple years
		
		ArrayList<String> years = new ArrayList<String>();
		;
		
		String y1 = d.toString().substring(0, 4);
		years.add(y1);
		String mon = d.toString().substring(5,7);
		String day = d.toString().substring(8,10);
		
		if(mon.equals("12")&&day.equals("31")){
			//System.out.println("$&$&in add dates");
			int int_y1 = Integer.parseInt(y1);
			//int int_y0 = int_y1-1;
			int int_y2 = int_y1+1;
			//String y0 = Integer.toString(int_y0);
			String y2 = Integer.toString(int_y2);
			//years.add(y0);
			years.add(y2);
			
		}
		return years;
		
	}
	
	public static String yearFromTripleDate(String d1){
		String p1 = "[0-9]{4}";
		Pattern pattern = Pattern.compile(p1);
		Matcher matcher = pattern.matcher(d1);
		
		if( matcher.find()){
			return matcher.group();
		}
		return null;
		
		
	}
	
	public static boolean playsForDateMatch(String d1, Timestamp d2){
		
		ArrayList<String> years = timeStamp2dates(d2);
		if( years.size()==0){
			return false;
			
		}
		
		String year1 = yearFromTripleDate(d1);
		if( year1==null){
			return false;
		}
		
	
		
		for( String year:years){
			if(year.equals(year1)) {
				return true;
			}
		}
		return false;

	}
	//e1 is the Triple entity
	//e2 is the yago entity
	public static boolean playsForEntityMatch(String e1, String e2){
		
		
		//could try subStringMatch(e1,e2)||subStringMatch(e2,e1)
		return subStringMatch(e1,e2);
	}
	
	
	public static ArrayList<String> str2list(String s){
		ArrayList<String> list =new ArrayList<String>();
		String[] ss = s.split("");
		for(String a:ss){
			list.add(a);
		}
		return list;
	}
	
	public static int indexOf(String s,ArrayList<String> ts, int start){
		if( start>=ts.size()){
			return -1;
		}
		int i =0;
		if( start<0){
			i =0;
		}else{
			i = start;
		}
		
		for(int j=i;j<ts.size();j++){
			if( strequal(ts.get(j),s)){
				//System.out.println(ts.get(j)+","+s);
				return j;
			}
		}
		return -1;
		
	}
	public static boolean find(ArrayList<String>ss,ArrayList<String> ts){
		
		
		int start = -1;
		String start_word = ss.get(0);
		//System.out.println("start word:"+start_word);
		start = indexOf( start_word,ts,start);
		//System.out.println("start:"+start);
		while( start!=-1 ){
			//if(findOne(ss,ts,start)){
			if(findOne_1(ss,ts,start)){
				return true;
			}else{
				start = indexOf(start_word,ts,start+1);
			}
		}
		
		
		return false;
	}
	
	// a is sub b is master
	//basic idea is like SentencLookUp
	//Manchester City
	//Manchester_City_F.C.
	// should be treated as match
	public static boolean subStringMatch(String a, String b){
		
		a = a.replace(" ", "");
		//b = b.replace("_", "");
		b = IndexTACReader.convert(b);
		b = b.replace("_", "");
		a = a.toLowerCase();
		b = b.toLowerCase();
		
		ArrayList<String> as = str2list(a);
		ArrayList<String> bs = str2list(b);
		
		return find(as,bs);
		
		
	}
	//after finding the jth word in text is equal to the start word of sentence, then check wether words in sentence could be found sequentially in this sentence
		//allows one error
		public static boolean findOne_1(ArrayList<String> ss,ArrayList<String> ts, int start){
				
				int winsize =1;
				int error_count=0;
				//System.out.println("in findone:"+start);
				int win =start;//win is the center of the window
				for( int i =0; i<ss.size();i++){
					String word = ss.get(i);
					int res = windowContain(word,win,winsize,ts);
					if( res==-1000 ){
						error_count++;
					}
					
					if( (error_count>=3)&&(res ==-1000) ){
						return false;
					}else{
						if( res==-1000){
							win = win+1;
						}else{
							win = win+res+1;
						}
					}
					
				}
				
				return true;
			}
		
		//0 represent winceter match
		//+ represent wincenter+ match
		//- represent wincetern- match
		//-1000, no match
		public static int windowContain(String word, int win_center, int win_size,ArrayList<String>ts){
			
			if(win_center>=ts.size()){
				return -1000;
			}
			
			if( strequal(ts.get(win_center),word)){
				return 0;
			}
			
			int left = win_center-win_size;
			if( left<0){
				left =0;
			}
			
			int right = win_center+win_size;
			if( right>=ts.size()){
				right = ts.size()-1;
			}
			
			for( int m =left;m<=right;m++){
				String tword = ts.get(m);
				if (strequal(tword,word)){
					return m-win_center;
				}
			}
			return -1000;
		}
		
		public static boolean strequal(String s1,String s2){
			return s1.equals(s2);
		}
	
	public static void main(String[]args){
		//String path = "/largedata1/cmput696/students/lyao1/sogodata/1data/texts/";
		//String path1 ="/largedata1/cmput696/students/lyao1/sogodata/1data/texts/"+"erwerxxer.txt";
		//int i = path.length();
		//System.out.println(path1.substring(i, path1.length()-4));
		String s1 = "Cambridge";
		String s2 ="Cambridge_United_F\u002eC\u002e";
		System.out.println(Match.subStringMatch(s1, s2));
	}
	
}
